package com.ruoyi.shop.goods.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.shop.goods.domain.ShopAttr;
import com.ruoyi.shop.goods.domain.ShopGoodsModel;
import com.ruoyi.shop.goods.mapper.ShopGoodsModelMapper;
import com.ruoyi.shop.goods.service.IShopGoodsModelService;
import com.ruoyi.shop.goods.vo.ShopGoodsModelVo;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;

/**
 * 商品模型Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-26
 */
@Service
public class ShopGoodsModelServiceImpl implements IShopGoodsModelService {
	@Autowired
	private ShopGoodsModelMapper shopGoodsModelMapper;

	/**
	 * 查询商品模型
	 * 
	 * @param modelId 商品模型主键
	 * @return 商品模型
	 */
	@Override
	public ShopGoodsModel selectModelById(Long modelId) {
		return shopGoodsModelMapper.selectModelById(modelId);
	}

	/**
	 * 查询商品模型列表
	 * 
	 * @param goodsModel 商品模型
	 * @return 商品模型
	 */
	@Override
	public List<ShopGoodsModel> selectModelList(ShopGoodsModel goodsModel) {
		return shopGoodsModelMapper.selectModelList(goodsModel);
	}

	/**
	 * 新增商品模型
	 * 
	 * @param goodsModel 商品模型
	 * @return 结果
	 */
	@Override
	public int insertGoodsModel(ShopGoodsModelVo goodsModelVo) {
		ShopGoodsModel goodsModel = goodsModelVo;
		List<ShopAttr> specList = goodsModelVo.getSpecList();
		JSONArray specArray = JSONUtil.createArray();
		for (ShopAttr shopAttr : specList) {
			if (StringUtils.isNotNull(shopAttr.getAttrName())) {
				JSONObject specObject = JSONUtil.createObj();
				specObject.set("key", shopAttr.getAttrName());
				specObject.set("values", shopAttr.getAttrValues().split(","));
				specObject.set("sort", shopAttr.getSort());
				specArray.add(specObject);
			}
		}
		goodsModel.setSpecValue(trim(specArray.toString()));
		goodsModel.setSpecCount(specArray.size());
		goodsModel.setUpdateTime(DateUtils.getNowDate());
		return shopGoodsModelMapper.insertGoodsModel(goodsModel);
	}

	/**
	 * 修改商品类型
	 * 
	 * @param goodsModel 商品类型
	 * @return 结果
	 */
	@Override
	public int updateGoodsModel(ShopGoodsModel goodsModel) {
		goodsModel.setUpdateTime(DateUtils.getNowDate());
		return shopGoodsModelMapper.updateGoodsModel(goodsModel);
	}

	/**
	 * 修改商品规格
	 * 
	 * @param goodsModelVo
	 * @return
	 */
	@Override
	public int updateGoodsSpec(ShopGoodsModelVo goodsModelVo) {
		ShopGoodsModel goodsModel = goodsModelVo;
		List<ShopAttr> specList = goodsModelVo.getSpecList();
		JSONArray specArray = JSONUtil.createArray();
		for (ShopAttr shopAttr : specList) {
			if (StringUtils.isNotNull(shopAttr.getAttrName())) {
				JSONObject specObject = new JSONObject();
				specObject.set("key", shopAttr.getAttrName());
				specObject.set("values", shopAttr.getAttrValues().split(","));
				specObject.set("sort", shopAttr.getSort());
				specArray.add(specObject);
			}
		}
		goodsModel.setSpecValue(trim(specArray.toString()));
		goodsModel.setSpecCount(specArray.size());
		goodsModel.setUpdateTime(DateUtils.getNowDate());
		return shopGoodsModelMapper.updateGoodsModel(goodsModel);
	}

	/**
	 * 批量删除商品模型
	 * 
	 * @param modelIds 需要删除的商品模型主键
	 * @return 结果
	 */
	@Override
	public int deleteModelByIds(String modelIds) {
		return shopGoodsModelMapper.deleteModelByIds(Convert.toStrArray(modelIds));
	}

	/**
	 * 字符串处理
	 * 
	 * @param value
	 * @return
	 */
	private String trim(String value) {
		value = value.replace("，", ","); // 中文逗号转换
		value = StringUtils.deleteWhitespace(value);// 去除空格
		return value;
	}
	
	/**
	 * 查询所有商品模型
	 * 
	 * @return
	 */
	@Override
	public List<ShopGoodsModel> queryModels() {
		return shopGoodsModelMapper.selectModelList(null);
	}
}
